<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 工作分类模块
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\base\controller\Base;
use app\admin\model\Vocation as VocationModel;
use think\Db;

class Vocation extends Base
{
  public function lst()
  {
    $voc = Db::name('vocation')->paginate(20);
    $this->assign('voc',$voc);
    return view();
  }

  public function addEdit()
  {
    //获取管理员id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    $model = new VocationModel();
    if($id > 0) {
      //是修改操作
      if($this->request->isPost()) {
        $post = $this->request->post();
        $result = $model->edit($post,$id);
        if($result) {
          return $this->success('修改分类成功','admin/Vocation/lst');
        } else {
          return $this->error('修改失败');
        }
      } else {
        $info = $model->where('id','=',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
      }
    } else {
      //是新增操作
      if($this->request->isPost()) {
        $post = $this->request->post();
        $result = $model->add($post);
        if($result) {
          return $this->success('添加分类成功','admin/Vocation/lst');
        } else {
          return $this->error('添加分类失败');
        }
      } else {
        return $this->fetch();
      }
    }
  }

  public function delete()
  {
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if($id > 0) {
        if(false == Db::name('vocation')->where('id',$id)->delete()) {
          return $this->error('删除失败');
        } else {
          return $this->success('删除成功','admin/Vocation/lst');
        }
      } else {
        return $this->error('id不正确');
      }
    }
  }
}
?>